package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

//    添加角色
    @PostMapping
 public Result add(@RequestBody Role role){
     log.info("添加角色:{}",role);
     roleService.add(role);
     return Result.success();
 }

// 列表查询
    @GetMapping
    public Result list(String name, String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询列表:{},{},{},{}",name,label,page,pageSize);
//        调用Service分页查询
        PageResult<Role> pageResult = roleService.list( name, label, page, pageSize);
        return Result.success(pageResult);
    }

//    根据ID
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
//     System.out.println("查询id:"+id);
    log.info("查询id:{}",id);
    Role role = roleService.getById(id);
    return Result.success(role);
}

//修改角色
@PutMapping
public Result update(@RequestBody Role role){
    log.info("修改角色:{}",role);
    roleService.update(role);
    return Result.success();
}

//删除角色
@DeleteMapping("/{id}")
public Result deleteById(@PathVariable Integer id){
//     System.out.println("删除部门:"+id);
    log.info("删除角色:{}",id);
    roleService.deleteById(id);
    return Result.success();
}

//查询所有角色
    @GetMapping("/list")
    public Result select(){
//     System.out.println("查询所有角色");
     log.info("查询所有角色" );
     List<Role> roleList = roleService.selectAll();
     return Result.success(roleList);
 }
}
